﻿<%@ WebHandler Language="C#" Class="AjaxPost" %>

using System;
using System.Web;
using DflyingForum.Core.Framework;
using DflyingForum.Core.Framework.DflyingPortal;

public class AjaxPost : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";
        Core.Web.Code.ViewManager<Core.Web.Code.UI.Controls.ChildControls.PostList> viewManager =
            new Core.Web.Code.ViewManager<Core.Web.Code.UI.Controls.ChildControls.PostList>();
        var control = viewManager.LoadViewControl("~/Skins/MyTheme/Controls/ChildControls/PostList.ascx");

        InitControlData(control);

        string htmlPage = HtmlPager.GetPager(control.PageIndex, 1000, control.PageSize, "PageAjax", new string[0]);
        string postsList = viewManager.RenderView(control);

        context.Response.Write(Json.Serialize(new string[] { htmlPage, postsList }));
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    private void InitControlData(Core.Web.Code.UI.Controls.ChildControls.PostList control)
    {
        string[] urldata = Query.Request("urldata").Split(',');

        string sort = "";

        switch (urldata[2])//排序
        {
            case "sort0":
                sort = "Sort";
                break;
            case "sort1":
                sort = "CreateTime";
                break;
            case "sort2":
                sort = "ReplyCount";
                break;
            case "sort3":
                sort = "Hits";
                break;
            default:
                sort = "Sort";
                break;
        }

        DateTime time = DefaultValue.DefTime;
        switch (urldata[3])//时间
        {
            case "time0":
                time = DefaultValue.DefTime;
                break;
            case "time1":
                time = DateTime.Now.AddDays(-1);
                break;
            case "time2":
                time = DateTime.Now.AddDays(-2);
                break;
            case "time3":
                time = DateTime.Now.AddDays(-7);
                break;
            case "time4":
                time = DateTime.Now.AddMonths(-1);
                break;
            case "time5":
                time = DateTime.Now.AddMonths(-3);
                break;
            default:
                time = DefaultValue.DefTime;
                break;
        }

        control.TopicId = Convert.ToInt32(urldata[0]);
        control.ModuleId = Convert.ToInt32(urldata[1]);
        control.SortBy = sort;
        control.SelectTime = time;
        control.StateId = Convert.ToInt32(urldata[6]);
        control.PageIndex = Int32.Parse(urldata[5]);
        control.PageSize = DefaultValue.PageSize;
    }
}